package by.home;


import java.util.Scanner;

public class App {

    private EnvironmentManipulator environmentManipulator = new EnvironmentManipulator();

    public static void main(String args[]) {

        App app = new App();

        EnvironmentManipulator environmentManipulator = app.getEnvironmentManipulator();
        environmentManipulator.createMap(10, 10);

        try {
            environmentManipulator.createTreasure(9, 9);
            environmentManipulator.createTreasureFinder(1, 1);
        } catch (PlacingOnMapException e) {
            System.out.println(e.getMessage());
            System.exit(1);
        }

        environmentManipulator.createMountains(2);

        System.out.println("Please, choose direction: 1. up; 2. down; 3. left; 4. right; 5. EXIT");

        while (true) {
            try {
                switch (app.getNewDirection()) {
                    case 1:
                        if (environmentManipulator.moveFinderUp()) {
                            System.out.println("Now you're closer to the treasure");
                        } else {
                            System.out.println("Now you're farther to the treasure");
                        }

                        break;
                    case 2:
                        if (environmentManipulator.moveFinderDown()) {
                            System.out.println("Now you're closer to the treasure");
                        } else {
                            System.out.println("Now you're farther to the treasure");
                        }
                        break;
                    case 3:
                        if (environmentManipulator.moveFinderLeft()) {
                            System.out.println("Now you're closer to the treasure");
                        } else {
                            System.out.println("Now you're farther to the treasure");
                        }
                        break;
                    case 4:
                        if (environmentManipulator.moveFinderRight()) {
                            System.out.println("Now you're closer to the treasure");
                        } else {
                            System.out.println("Now you're farther to the treasure");
                        }
                        break;

                    case 5:
                        System.out.println("Goodbye !");
                        System.exit(1);
                    default:
                        System.out.println("Please check the symbols you've entered");
                        break;
                }
            } catch (OutOfMapBordersException e) {
                System.out.println(e.getMessage());
                continue;
            } catch (PlacingOnMapException e) {
                System.out.println(e.getMessage());
                continue;
            }

            if (environmentManipulator.checkIfFounderFoundTheTreasure()) {
                System.out.println("Congratulations, you've found the treasure");
                break;
            }
        }
    }

    public EnvironmentManipulator getEnvironmentManipulator() {
        return environmentManipulator;
    }

    public void setEnvironmentManipulator(EnvironmentManipulator environmentManipulator) {
        this.environmentManipulator = environmentManipulator;
    }

    public int getNewDirection() {
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            return scanner.nextInt();
        } else {
            return 0;
        }
    }

}
